package jianzhi;

/**
 * description:
 * author:zt
 * date:2021-10-19
 */

/**
 * 统计一个数字在排序数组中出现的次数。
 */
public class jianzhi53 {
    public int search(int[] nums, int target) {
        int L = 0, R = nums.length-1;
        //找比target大的第一个数的位置
        while (L <= R){
            int mid = (L + R) >> 1;
            if (nums[mid] <= target) L = mid + 1;
            else R = mid - 1;
        }
        int right = L;
        if (R >= 0 && nums[R] != target) return 0;
        L = 0; R = nums.length-1;
        //找target第一次出现的位置
        while (L <= R){
            int mid = (L + R) >> 1;
            if (nums[mid] < target) L = mid + 1;
            else R = mid - 1;
        }
        int left = R;

        return right-left-1;
    }
}
